import 'package:bloc_usage/bloc/BlocProvider.dart';
import 'package:bloc_usage/bloc/MainBloc.dart';
import 'package:bloc_usage/model/entities.dart';
import 'package:bloc_usage/widget/article_item.dart';
import 'package:flutter/material.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with AutomaticKeepAliveClientMixin {
  MainBloc mainBloc;

  @override
  Widget build(BuildContext context) {
    mainBloc = BlocProvider.of<MainBloc>(context);
    mainBloc.getArticleList(0);
    return StreamBuilder<MainModel>(
      stream: mainBloc.mainStream,
      builder: (context, snapshot) {
        if (snapshot.data == null)
          return SizedBox.expand(
            child: Center(
              child: CircularProgressIndicator(
                strokeWidth: 2,
              ),
            ),
          );
        else
          return ListView(
            children: <Widget>[
              // Swiper(
              //   circular: true,
              //   autoStart: true,
              //   interval: Duration(seconds: 2),
              //   //banner images
              //   children: snapshot.data.banners.map((banner) {
              //     return Image.network(banner.imagePath);
              //   }).toList(),
              // ),
              ListView.builder(
                itemCount: snapshot.data.articles.length,
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return ArticeItem(article: snapshot.data.articles[index]);
                },
              )
            ],
          );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
